package com.seari.utils;

import java.util.Calendar;
import java.util.Date;

import com.seari.enums.WeekDayEnum;
import com.seari.web.vo.CronVO;

public class CronConverUtil {
	
	private static final String DAY_INTERVAL = "每天 ";
	private static final String WEEK_INTERVAL = "每周 ";
	
	public  CronVO CronConversion(String interval,int date,String dt,Integer fixedType,Integer fixedTime) {
		switch (interval) {
		case "day":
			CronVO cronVODay = CronConversionDay(dt);
			return cronVODay;
			
		case "week":
			CronVO cronVOWeek = CronConversionWeek(date,dt);
			return cronVOWeek;
		case "fixed":
			CronVO cronVOFixed = CronConversionFixed(fixedType,fixedTime);
			return cronVOFixed;
		}
		return null;
	}
	
	
	private  CronVO CronConversionDay(String dt) {
		CronVO cronVO = new CronVO();
		
		String[] splitTime = splitTime(dt);
		String cron = "0 " + splitTime[1] + " " + splitTime[0] + " * * ?";
		
		String cronDesc = DAY_INTERVAL + dt + " 执行";
		cronVO.setCron(cron);
		cronVO.setCronDesc(cronDesc);
		return cronVO;
	}
	
	private  CronVO CronConversionWeek(int date,String dt) {
		CronVO cronVO = new CronVO();
		
		String dateName = WeekDayEnum.getWeekDayEnumByCode(date).name();
		String[] splitTime = splitTime(dt);
		String cron = "0 " + splitTime[1] + " " + splitTime[0] + " ? * " + dateName;
		
		String cronDesc = WEEK_INTERVAL + WeekDayEnum.getWeekDayEnumByCode(date).getValue() + " " + dt + " 执行";
		
		cronVO.setCron(cron);
		cronVO.setCronDesc(cronDesc);
		return cronVO;
	}
	
	private  CronVO CronConversionFixed(Integer fixedType,Integer fixedTime) {
		CronVO cronVO = new CronVO();
		String cron = null;
		String cronDesc = null;
		// 1:分钟  2:小时
		switch (fixedType) {
		case 1:
			cron = "0 " + "0/" + fixedTime + " * * * ?";
			cronDesc = "每 " + fixedTime + " 分钟 执行一次";
			break;
		case 2:
			int minutes = this.getCurrentMin(); 
			cron = "0 " + minutes + " 0/" + fixedTime + " * * ?";  // 0 0 0/2 * * ? 
			cronDesc = "每 " + fixedTime + " 小时 执行一次";
			break;
		default:
			return null;
		}
		cronVO.setCron(cron);
		cronVO.setCronDesc(cronDesc);
		return cronVO;
	}
	
	private String[] splitTime(String dt) {
		String[] split = dt.split(":");
		for (int i = 0; i < split.length; i++) {
			if(split[i].startsWith("0")) {
				split[i] = cutZero(split[i]);
			}
		}
		return split;
	}
	
	private String cutZero(String dt) {
		return dt.replaceFirst("0", "");
	}
	
	private Integer getCurrentMin() {
		Date date = new Date();
	    Calendar calendar = Calendar.getInstance();
	    calendar.setTime(date);
	    return calendar.get(Calendar.MINUTE);
	}
	
	
	
	 
	public static void main(String[] args) {
//		String dt = "01:01";
//		int date = 2;
		CronConverUtil util = new CronConverUtil();
//		CronVO cronVO = util.CronConversionDay(dt);
//		System.out.println(cronVO.getCron());
//		System.out.println(cronVO.getCronDesc());
//		Integer currentMin = util.getCurrentMin();
//		System.out.println(currentMin);
		CronVO cronVO = util.CronConversionFixed(2, 1);
		System.out.println(cronVO.getCron());
		System.out.println(cronVO.getCronDesc());
	}
	
	
}
